package com.zlq.cn.service.ai;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlq.cn.api.ResponseException;
import com.zlq.cn.bootstrap.SignApiBootstrap;
import com.zlq.cn.config.Config;
import com.zlq.cn.dto.param.TencentAiParam;
import com.zlq.cn.entity.FanyiUser;
import com.zlq.cn.entity.TencentAi;
import com.zlq.cn.mapper.ai.TencentAiMapper;
import com.zlq.cn.service.un.ConfigService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @author zhoucl
 * @date 2023年04月03日 14:24
 */
@AllArgsConstructor
@Service("aiCommonService")
public class AiCommonService extends ServiceImpl<TencentAiMapper, TencentAi> {

    private final ConfigService configService;

    public String saveTencentAi(String project, String type, String beforeImage, String afterImage) {
        FanyiUser fanyiUser = SignApiBootstrap.requestFanYiUser(Config.PROJECT_AI);
        TencentAi tencentAi = new TencentAi()
                .setUserImei(fanyiUser.getFanyiUserImei())
                .setShowImage("https://thirtyfairy.oss-cn-shenzhen.aliyuncs.com/finder/F1657E5F-C540-4145-B70A-7299F4C69259.jpg")
                .setBeforeImage(beforeImage)
                .setAfterImage(afterImage)
                .setType(type)
                .setProject(project)
                .setCreateTime(LocalDateTime.now());
        super.save(tencentAi);
        return tencentAi.getAfterImage();
    }

    public Boolean deleteImage(TencentAi param) {
        FanyiUser fanyiUser = SignApiBootstrap.requestFanYiUser(Config.PROJECT_AI);
        super.lambdaQuery()
                .eq(TencentAi::getId, param.getId())
                .eq(TencentAi::getUserImei, fanyiUser.getFanyiUserImei())
                .eq(TencentAi::getProject, param.getProject())
                .oneOpt()
                .ifPresent((tencentAi) -> {
                    super.removeById(tencentAi.getId());
                    configService.deleteObject(tencentAi.getAfterImage());
                });
        return Boolean.TRUE;
    }

    public FanyiUser checkUserNum(String project) {
        return checkUserNum(project, null, null);
    }

    public FanyiUser checkUserNum(String project, String type, Integer maxCut) {
        FanyiUser fanyiUser = SignApiBootstrap.requestFanYiUser();
        if (fanyiUser.getSurplusDay() <= 0) {
            ResponseException.resultError("300", "非VIP无法使用当前功能");
        }
        if (type != null && maxCut != null) {
            LocalDate now = LocalDate.now();
            LocalDateTime begin = LocalDateTime.of(now, LocalTime.of(0, 0, 0));
            LocalDateTime end = LocalDateTime.of(now, LocalTime.of(23, 59, 59));
            Integer count = super.lambdaQuery()
                    .eq(TencentAi::getUserImei, fanyiUser.getFanyiUserImei())
                    .eq(TencentAi::getType, type)
                    .between(TencentAi::getCreateTime, begin, end)
                    .count();
            if (count != null && count > maxCut) {
                ResponseException.resultApiError("今日次数已使用完,请明日再来哦");
            }
        }
        return fanyiUser;
    }

    public Page<TencentAi> imagePage(Page<TencentAi> page, TencentAiParam tencentAi) {
        FanyiUser fanyiUser = SignApiBootstrap.requestFanYiUser(tencentAi.getProject());
        return super.lambdaQuery()
                .eq(TencentAi::getUserImei, fanyiUser.getFanyiUserImei())
                .eq(StringUtils.isNotEmpty(tencentAi.getType()), TencentAi::getType, tencentAi.getType())
                .eq(TencentAi::getProject, tencentAi.getProject())
                .orderByDesc(TencentAi::getCreateTime)
                .page(page);
    }
}
